package cn.ewan.cloud.tenant.data.router.example.controller;

import cn.ewan.cloud.common.support.Log;
import cn.ewan.cloud.common.support.utils.AsyncUtil;
import cn.ewan.cloud.common.web.support.rocketmq.BaseRocketMQConsumer;
import cn.ewan.cloud.tenant.data.router.core.common.util.TenantLog;
import cn.ewan.cloud.tenant.data.router.mq.core.TenantRocketMQConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.springframework.stereotype.Component;


@Component
@RocketMQMessageListener(topic = TestController.topic, consumerGroup = "tenant-consume")
public class MQConsumer extends TenantRocketMQConsumer {


    @Override
    protected void consume(MessageExt message, String body) {
        Log.NORMAL.info("msg={},body={}", message, body);
        AsyncUtil.run(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            TenantLog.log("test mq consumer async");
        });
    }


    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.setMaxReconsumeTimes(4);
        consumer.setConsumeThreadMax(10);
        consumer.setConsumeThreadMin(10);
    }
}
